﻿Public Class frmTableSingleList

    Dim oTableSingleInfo As Data.TableSingleInfo

    Public Function Initialize(ByRef inTableSingleInfo As Data.TableSingleInfo) As Boolean
        Try
            oTableSingleInfo = inTableSingleInfo
            Me.Text = inTableSingleInfo.strNameForm
            Me.Tag = inTableSingleInfo.intType
            objList.NameList = inTableSingleInfo.strNameForm
            objList.NameForm = inTableSingleInfo.strNameForm

            objList.AddButtonStandard()
            grdGrid.AddMenuStandard()
            grdGrid.NomeElementoPerEsportazione = inTableSingleInfo.strNameElement
            grdGrid.NomeCampoRicercaDaTastiera = "Name"

            Return LoadGrid("")
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Grid"

    Private Function LoadGrid(ByVal inSqlWhere As String) As Boolean
        Try
            Dim strSqlWhere As String = IIf(inSqlWhere = "", "", "Where " & inSqlWhere)
            Dim strSql As String = String.Format("Select * From {0} {1} Order by Name", oTableSingleInfo.strNameTable, strSqlWhere)
            Using daAdapt As New MySqlDataAdapter(strSql, gConnectionStringAgenda.ToString)
                Using dtTable As New DataTable(oTableSingleInfo.strNameTable)
                    daAdapt.Fill(dtTable)
                    grdGrid.DataSource = dtTable
                    grdGrid.Columns("ID").Visible = False
                    grdGrid.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    Return True
                End Using
            End Using
        Catch ex As Exception
            Base.ShowError.Show("Load Grid", Me.Name, "LoadGrid", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Commands"

    Private Sub Delete() Handles objList.OnDelete, grdGrid.OnElimina
        Try
            If grdGrid.CurrentRow IsNot Nothing Then
                Me.Cursor = Cursors.WaitCursor
                Me.Enabled = False

                Dim intID As Integer = Utility.ConvStrNum(grdGrid.CurrentRow.Cells("ID").Value)
                Dim oTableSingle As New Data.TableSingle
                oTableSingle.strNameElementComplete = grdGrid.CurrentRow.Cells("Name").Value
                oTableSingle.strNameTable = oTableSingleInfo.strNameTable
                If oTableSingle.IsPossibleDelete() Then
                    If oTableSingle.Delete(intID) Then
                        Call LoadGrid("")
                    Else
                        MsgBox("Impossible delete selected element.", vbExclamation, Me.Text)
                    End If
                End If
            End If
        Catch ex As Exception
            Base.ShowError.Show("Delete", Me.Name, "Delete", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Insert() Handles objList.OnInsert, grdGrid.OnInserisci
        Try
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False

            Dim ofrmTableSingleInsertion As New frmTableSingleInsert
            If ofrmTableSingleInsertion.Initialize(oTableSingleInfo) Then
                If ofrmTableSingleInsertion.ShowDialog = vbOK Then
                    Call LoadGrid("")
                    grdGrid.SelezionaRow(ofrmTableSingleInsertion.mId)
                End If
            End If
            ofrmTableSingleInsertion = Nothing

        Catch ex As Exception
            Base.ShowError.Show("Insert", Me.Name, "Insert", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Modify() Handles objList.OnModify, grdGrid.OnModifica
        Try
            If grdGrid.CurrentRow IsNot Nothing Then
                Me.Cursor = Cursors.WaitCursor
                Me.Enabled = False

                Dim intID As Integer = Utility.ConvStrNum(grdGrid.CurrentRow.Cells("ID").Value)
                Dim ofrmTableSingleModify As New frmTableSingleModify
                If ofrmTableSingleModify.Initialize(intID, oTableSingleInfo) Then
                    If ofrmTableSingleModify.ShowDialog = vbOK Then
                        Call LoadGrid("")
                        grdGrid.SelezionaRow(intID)
                    End If
                End If
                ofrmTableSingleModify = Nothing

            End If
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "Modify", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Search() Handles objList.OnSearch, grdGrid.OnRicerca
        Try
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False
            Dim ofrmTableSingleSearch As New frmTableSingleSearch
            If ofrmTableSingleSearch.Initialize(oTableSingleInfo) Then
                If ofrmTableSingleSearch.ShowDialog = vbOK Then
                    Call LoadGrid(ofrmTableSingleSearch.strSqlWhere)
                End If
            End If
            ofrmTableSingleSearch = Nothing

        Catch ex As Exception
            Base.ShowError.Show("Search", Me.Name, "Search", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    'Private Sub Stampa() Handles objList.OnStampa, grdGrid.OnStampa
    '    Try
    '        Me.Cursor = Cursors.WaitCursor
    '        Me.Enabled = False

    '        Dim oReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    '        oReportDocument.Load(GetSetting(Application.ProductName, "Impostazioni", "PathReport", "") & "\rptTableSingle.rpt", CrystalDecisions.[Shared].OpenReportMethod.OpenReportByDefault)
    '        oReportDocument.SetDataSource(CType(grdGrid.DataSource, DataTable).DefaultView)
    '        Utility.InitializeReport(oReportDocument)
    '        oReportDocument.SetParameterValue("txtTitolo", oTableSingleInfo.strNameForm)
    '        oReportDocument.PrintToPrinter(1, False, 0, 0)


    '    Catch ex As Exception
    '        Base.ShowError.Show("Stampa", Me.Name, "Stampa", ex)
    '    Finally
    '        Me.Cursor = Cursors.Default
    '        Me.Enabled = True
    '    End Try
    'End Sub

    Private Sub CloseForm() Handles objList.OnClose
        Me.Close()
    End Sub

#End Region

#Region "Search"

    Private Sub objList_OnFilterSearch(ByVal inText As String) Handles objList.OnFilterSearch
        Try
            CType(grdGrid.DataSource, DataTable).DefaultView.RowFilter = String.Format("Name like '{0}%'", inText)
        Catch ex As Exception
            Base.ShowError.Show("Rapid Search", Me.Name, "objList_OnFilterSearch", ex)
        End Try
    End Sub

#End Region

End Class